package com.example.springbootcaffeinedemo.common2;
import com.example.springbootcaffeinedemo.common1.Person;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/04/18  14:43
 * @Description: 测试CacheStats缓存的各项指标-使用默认的缓存状态收集器
 */
public class CacheStatsDemo1 {
    public static void main(String[] args) {
        Caffeine<String, Person> caffeine = Caffeine.newBuilder()
                .maximumWeight(30)
                .recordStats()
                // 设置权重值规则
                .weigher((String key, Person value) -> value.getAge());
        Cache<String, Person> cache = caffeine.build();

        cache.put("one",new Person(12,"one"));
        cache.put("two",new Person(3,"two"));
        cache.put("three",new Person(18,"three"));

        Person one = cache.getIfPresent("one");
        System.out.println("one: " +one);
        CacheStats cacheStats = cache.stats();
        System.out.println("cacheStats: " +cacheStats);
        System.out.println("命中缓存的次数: " +cacheStats.hitCount());
    }
}
